/**
 * 此模块用于维护用户数据
 */

import { getUserInformation, logout } from '@/api/user'
import { removeToken } from '@/utils/token'
import { Message } from 'element-ui'

import router from '@/router'

const state = {
  // 用户数据
  userList: {}
}

const mutations = {
  // 保存用户数据
  saveUser (state, params) {
    state.userList = params
  }
}

const actions = {
  // 获取用户数据
  async userInformation ({ commit }) {
    const res = await getUserInformation()
    if (res.code === 200) {
      commit('saveUser', res.data)
    }
  },
  // 退出登录
  async _g_log_out ({ commit }, message) {
    const res = await logout()
    if (res.code === 200) {
      removeToken()
      router.push('/login')
      commit('saveUser', {})
      Message.success(message)
    }
  }
}

const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
